package com.example.sharding.mapper;

import com.baomidou.dynamic.datasource.annotation.DS;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.example.sharding.config.DataSourceConfig;
import com.example.sharding.dto.UserVo;
import com.example.sharding.model.entity.User;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import java.time.LocalDateTime;
import java.util.Date;
import java.util.List;

@DS(DataSourceConfig.SHARDING_DATA_SOURCE_NAME)
public interface UserMapper extends BaseMapper<User> {
    @Select("<script>" +
            "SELECT  convert(nvarchar(10),create_time,120) as udate, \n" +
            "sum(case when sex='男' then 1 else 0 end)  as manNun,\n" +
            "sum(case when sex='女' then 1 else 0 end)  as womanNum \n" +
            "FROM tb_user \n" +
            "where create_time &gt;= #{startTime} and  create_time &lt; #{endTime}  \n" +
            "GROUP BY convert(nvarchar(10),create_time,120) " +
            "</script>")
    List<UserVo> selectUser(@Param("startTime") Date startTime, @Param("endTime") Date endTime);

    @Select("<script>" +
            "SELECT  COUNT(1) as num \n" +
            "FROM tb_user \n" +
            "where create_time &gt;= #{startTime} and  create_time &lt; #{endTime}  \n" +
            "</script>")
    int getCount(@Param("startTime") Date startTime, @Param("endTime") Date endTime);

    @Select("<script>" +
            "SELECT  * \n" +
            "FROM tb_user \n" +
            "where create_time &gt;= #{startTime} and  create_time &lt; #{endTime}  \n" +
            "ORDER BY id desc OFFSET #{start} ROWS FETCH NEXT #{size} ROWS ONLY " +
            "</script>")
    List<User> getPage(@Param("startTime") Date startTime, @Param("endTime") Date endTime, @Param("start") Integer start, @Param("size") Integer size);
}